/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * <p>Project: jdbc202202 - M8Demo
 * <p>Powered by webrx On 2022-08-19 16:37:36
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class M8Demo {

    @Test @DisplayName("测试连接数据库mysql 8.0.30")
    void connect(){
        try{
            //1.加载mysql数据库jdbc驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.定义连接url字符串
            String url = "jdbc:mysql://localhost:3306/db?user=root";
            url = "jdbc:mysql://localhost:3305/mysql?useUnicode=true&characterEncoding=utf8&serverTimeZone=PRC&user=root";

            //3.建立连接
            //Connection conn = DriverManager.getConnection(url);
            //Connection conn = DriverManager.getConnection("jdbc:mysql://:3305","root","");
            //Connection conn = DriverManager.getConnection("jdbc:mysql:/","root","");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8&serverTimeZone=PRC","admin","");


            System.out.println(conn.getMetaData().getDatabaseProductVersion());

            //4.建立Satement语句
            Statement stmt = conn.createStatement();

            //5.执行sql语句
            ResultSet rs = stmt.executeQuery("show databases");

            //6.处理结果集
            while(rs.next()){
                System.out.println(rs.getString(1));
            }
            rs.close();
            stmt.close();
            conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    @Test @DisplayName("测试连接mysql 5.6.51")
    void conn(){
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            //var c = DriverManager.getConnection("jdbc:mysql://localhost:3305/db?user=root&password=");
            var c = DriverManager.getConnection("jdbc:mysql://localhost:3305/db","root","");
            System.out.println(c.getMetaData().getDatabaseProductVersion());
        }catch(Exception e){
            e.printStackTrace();
        }
    }


}
